﻿using System.Collections.Generic;
using System.Linq;
using PaceR.Core.Interfaces;

namespace PaceR.Core.Entities
{
    public class SimpleResultMessages : IResultMessages
    {
        private IResultObjectFactory _factory;

        public SimpleResultMessages(IResultObjectFactory factory)
        {
            _factory = factory;
            AllMessages = new List<IResultMessage>();
        }

        #region IResultMessages Members

        public IEnumerable<IResultMessage> AllMessages { get; set; }

        public IEnumerable<IResultMessage> InformationMessages
        {
            get { return AllMessages.Where(m => m.MessageType == ResultMessageTypes.Information); }
        }

        public IEnumerable<IResultMessage> WarningMessages
        {
            get { return AllMessages.Where(m => m.MessageType == ResultMessageTypes.Warning); }
        }

        public IEnumerable<IResultMessage> ErrorMessages
        {
            get { return AllMessages.Where(m => m.MessageType == ResultMessageTypes.Error); }
        }

        public void MergeMessages(IResultMessages resultMessages)
        {
            if (resultMessages != null)
                ((List<IResultMessage>)AllMessages).AddRange(resultMessages.AllMessages);
        }

        public void AddMessage(IResultMessage message)
        {
            ((List<IResultMessage>)AllMessages).Add(message);
        }

        public void AddMessage(string identifier, string message, ResultMessageTypes messageType)
        {
            var msg = _factory.GetResultMessageObject();
            msg.Identifier = identifier;
            msg.Message = message;
            msg.MessageType = messageType;
            AddMessage(msg);
        }

        public void AddInformationMessage(string identifier, string message)
        {
            AddMessage(identifier, message, ResultMessageTypes.Information);
        }

        public void AddWarningMessage(string identifier, string message)
        {
            AddMessage(identifier, message, ResultMessageTypes.Warning);
        }

        public void AddErrorMessage(string identifier, string message)
        {
            AddMessage(identifier, message, ResultMessageTypes.Error);
        }

        #endregion
    }
}
